﻿using Bear.Library.Util;
using Bear.Library.WinApi;
using Bear.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Bear.BLL.MapleStory
{
    public class Automatic : ScriptBase
    {
        public override void Run(DataCenter data)
        {
            data.Handle = WinApiHelper.FindWindow(null, data.Config.GameTitle);

            if (data.Handle == IntPtr.Zero)
            {
                this.OnShowMessage("启动游戏...");
                StartGame StartGame = new StartGame();
                StartGame.ShowMessage += OnShowMessage;
                StartGame.Run(data);
                return;
            }
            //设为前端窗口
            WinApiHelper.SetForegroundWindow(data.Handle);

            var rect = new Rect();
            WinApiHelper.GetClientRect(data.Handle, ref rect);
            if (rect.Right - rect.Left < 800 || rect.Bottom - rect.Top < 600)
            {
                this.OnShowMessage("窗口小于800x600..");
                KeyboardMouseOperator.KeyPress(System.Windows.Forms.Keys.Enter);
                Thread.Sleep(5000);
                return;
            }


            //获取图片
            using (Bitmap source = ScreenCapturer.Capture(data.Handle))
            {
                if (CvHelper.TemplateMatching(source, data.LoginImage, 0.9))
                {
                    this.OnShowMessage("登录游戏...");
                    Login login = new Login();
                    login.ShowMessage += OnShowMessage;
                    login.Run(data);
                }

                if (CvHelper.TemplateMatching(source, data.EnterImage, 0.9))
                {
                    this.OnShowMessage("已经进入游戏...");
                    Attack Attack = new Attack();
                    Attack.ShowMessage += OnShowMessage;
                    Attack.Run(data);                    
                    return;
                }


                Color color1 = source.GetPixel(50, 50);
                Color color2 = source.GetPixel(750, 550);
                if (color1.ToArgb() == -1 || color2.ToArgb() == -1 || color2.ToArgb() == -16777216)
                {
                    this.OnShowMessage("游戏进入，等待开场动画...");
                    Thread.Sleep(3000);
                    return;
                }








            }







            //游戏截图




        }
    }
}
